﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web.BaseInfoServiceRef;

    public class Employee : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlDepartmentID;
        protected DropDownList ddlPosition;
        protected DropDownList ddlSex;
        protected HtmlForm F2651;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected Label Label1;
        protected Label lblDepartmentID;
        protected Label lblEmployee;
        protected Label lblEmployeeID;
        protected Label lblEmployeeIDName;
        protected Label lblEntrantDate;
        protected Label lblMobilePhone;
        protected Label lblMsg;
        protected Label lblPosition;
        protected Label lblSex;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtEmployeeID;
        protected TextBox txtEmployeeIDName;
        protected TextBox txtEntrantDate;
        protected TextBox txtMobilePhone;
        protected TextBox txtRemark;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtEmployeeID.Text = "";
            this.txtEmployeeID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtEmployeeID.Text.Trim()))
            {
                try
                {
                    List<string> employeeID = new List<string> {
                        this.txtEmployeeID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(employeeID, "EMPLOYEE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("EmployeeQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("EmployeeList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtEmployeeID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2650_001");
            }
            else if (string.IsNullOrEmpty(this.txtEmployeeIDName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2650_002");
            }
            else if (string.IsNullOrEmpty(this.ddlDepartmentID.SelectedValue.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2650_003");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateEmployeeInfo();
            }
            else
            {
                this.InsertEmployeeInfo();
            }
        }

        private void InsertEmployeeInfo()
        {
            try
            {
                EmployeeInfo employeeInfo = new EmployeeInfo {
                    EmployeeID = this.txtEmployeeID.Text.Trim(),
                    EmployeeName = this.txtEmployeeIDName.Text.Trim(),
                    Sex = this.ddlSex.SelectedValue,
                    Position = this.ddlPosition.SelectedValue,
                    DepartmentID = this.ddlDepartmentID.SelectedValue,
                    MobilePhone = this.txtMobilePhone.Text.Trim(),
                    EntrantDate = this.txtEntrantDate.Text.Trim(),
                    Remark = this.txtRemark.Text.Trim(),
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(employeeInfo, "EMPLOYEE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = employeeInfo;
                this.txtEmployeeID.Enabled = false;
                base.Cache.Remove("Employee");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindDataDDLBySourceFrom(this.ddlDepartmentID, "DEPARTMENT");
            base.BindData2DDLBySysCode(this.ddlPosition, "EmployeePosition");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetEmployeeInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetEmployeeInfo2Form()
        {
            try
            {
                string employeeID = base.Request.QueryString["ID"].ToString();
                EmployeeInfo employeeInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(employeeID, "EMPLOYEE") as EmployeeInfo;
                base.DataEntityViewState = employeeInfo;
                if (employeeInfo != null)
                {
                    this.txtEmployeeID.Text = employeeInfo.EmployeeID;
                    this.txtEmployeeIDName.Text = employeeInfo.EmployeeName;
                    this.ddlPosition.SelectedValue = employeeInfo.Position;
                    this.ddlDepartmentID.SelectedValue = employeeInfo.DepartmentID;
                    this.ddlSex.SelectedValue = employeeInfo.Sex;
                    this.txtMobilePhone.Text = employeeInfo.MobilePhone;
                    this.txtEntrantDate.Text = employeeInfo.EntrantDate.ToString();
                    this.txtRemark.Text = employeeInfo.Remark;
                    this.chkIsActive.Checked = employeeInfo.IsActive == "Y";
                    this.txtEmployeeID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateEmployeeInfo()
        {
            try
            {
                EmployeeInfo employeeInfo = base.DataEntityViewState as EmployeeInfo;
                employeeInfo.EmployeeID = this.txtEmployeeID.Text.Trim();
                employeeInfo.EmployeeName = this.txtEmployeeIDName.Text.Trim();
                employeeInfo.Position = this.ddlPosition.SelectedValue;
                employeeInfo.DepartmentID = this.ddlDepartmentID.SelectedValue;
                employeeInfo.MobilePhone = this.txtMobilePhone.Text.Trim();
                employeeInfo.EntrantDate = this.txtEntrantDate.Text.Trim();
                employeeInfo.Remark = this.txtRemark.Text.Trim();
                employeeInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                employeeInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(employeeInfo, "EMPLOYEE") + base.GetGlobalMsgResource("C002");
                base.Cache.Remove("Employee");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

